package com.appli.JustinBieberClic;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Chronometer.OnChronometerTickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.scoreloop.client.android.ui.EntryScreenActivity;
import com.scoreloop.client.android.ui.OnScoreSubmitObserver;
import com.scoreloop.client.android.ui.ScoreloopManagerSingleton;
import com.scoreloop.client.android.ui.ShowResultOverlayActivity;

/**
 * @author AndroidMythicTeam
 * @version 14-04-2011 - Ajout du chronometre et gestion des preferences 
 * @version 19-04-2011 - Ajout du ScoreLoopManagement
 */
public class JustinBieberClic extends Activity implements OnClickListener, OnScoreSubmitObserver  {

	/* Variables */
	private Button boutonRejouer;
	private Button boutonScoreloopProfile;
	private Button boutonSubmitScore;
	private Chronometer chronometre;
	private ProgressDialog progressDialog;
	private ImageButton imageBoutonBieber;
	private TextView texteIntro;
	private TextView texteScore;
	private Boolean init = false;
	private Boolean finish = false;
	
	
	private Integer i = 0;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		boutonScoreloopProfile = ((Button)findViewById(R.id.btn_edit_profile));
		boutonSubmitScore = ((Button)findViewById(R.id.btn_submit_score));
		
		ScoreloopManagerSingleton.init(this);
		ScoreloopManagerSingleton.get().setOnScoreSubmitObserver(this);
		
		chronometre = (Chronometer)findViewById(R.id.chronometer1);
		chronometre.setOnChronometerTickListener(mOnChronometerTickListener);

		texteIntro = (TextView)findViewById(R.id.textView1);
		texteIntro.setText("0");

		texteScore = (TextView)findViewById(R.id.score);

		getPreference();
		/*		
		SharedPreferences settings = getSharedPreferences("user_score", Context.MODE_PRIVATE);
		if (settings.getString("score", "") == ""){
			texteScore.setText("No personal record");
			boutonSubmitScore.setVisibility(View.GONE);
		}else{
			texteScore.setText("Personal record " + settings.getString("score", ""));
		}*/

		imageBoutonBieber = (ImageButton)findViewById(R.id.imageButton1);
		imageBoutonBieber.setImageResource(R.drawable.bieber);
		
		// Bouton pour rejouer
		boutonRejouer = (Button)findViewById(R.id.replay);
		boutonRejouer.setVisibility(View.GONE);
		boutonRejouer.setOnClickListener(mResetListener);
		
		// Acquisition des boutons pour listeners
		((Button)findViewById(R.id.btn_submit_score)).setOnClickListener(this);
		((Button)findViewById(R.id.btn_edit_profile)).setOnClickListener(this);
		
		// Barre de progression pour ScoreLoop
		progressDialog = new ProgressDialog(this);
		progressDialog.setMessage("Submit on progress...");
		progressDialog.setCancelable(true);
		
		// Lecture MP3 du sample Audio
		MediaPlayer.create(this, R.raw.bieber).start();

	}
	
	public void onDestroy() {
		super.onDestroy();
        ScoreloopManagerSingleton.destroy();
	}
	
	/**
	 * Listener Chronometer
	 */
	OnChronometerTickListener mOnChronometerTickListener = new OnChronometerTickListener() {
		public synchronized void  onChronometerTick (Chronometer chronometer) {
			JustinBieberClic.this.i++;
	//		long elapsedMillis = SystemClock.elapsedRealtime() - chronometer.getBase();
			 
			if (JustinBieberClic.this.i == 1){
				Toast.makeText(JustinBieberClic.this, "LET'S GO !!!!!", Toast.LENGTH_SHORT).show();
			}else
				if (JustinBieberClic.this.i == 10){
					Toast.makeText(JustinBieberClic.this, "COME ON !!!!!", Toast.LENGTH_SHORT).show();
				}else
					if (JustinBieberClic.this.i == 20){
						Toast.makeText(JustinBieberClic.this, "FASTER !!!!!", Toast.LENGTH_SHORT).show();
					}else
						if (JustinBieberClic.this.i == 30){
							Toast.makeText(JustinBieberClic.this, "CONTINUE ?!?!", Toast.LENGTH_SHORT).show();
						}else	
							if (JustinBieberClic.this.i == 40){
								Toast.makeText(JustinBieberClic.this, "20 SECONDS LEFT !!!!!", Toast.LENGTH_SHORT).show();
							}else
								if (JustinBieberClic.this.i == 62){
									Toast.makeText(JustinBieberClic.this, "JUSTIIIIINNNN !", Toast.LENGTH_SHORT).show();
									finish = true;
									SaveScore();
									boutonRejouer.setVisibility(0);
									MediaPlayer.create(JustinBieberClic.this, R.raw.bieber).start();
									chronometre.stop();
									boutonScoreloopProfile.setVisibility(1);
									boutonSubmitScore.setVisibility(1);
								}
		}
	};

	public void OnImageClick(View v) {
		if (!finish) {
			if (init == false){
				start();			  
			}
			texteIntro.setText((Integer.valueOf(texteIntro.getText().toString())+1)+"");
		}
	}

	public void SaveScore() {
		chronometre.stop();
		addPreference();
		/*
		SharedPreferences settings = getSharedPreferences("user_score", Context.MODE_PRIVATE);
		if (settings.getString("score", "") == ""  || (Integer.valueOf(settings.getString("score", "")) < Integer.valueOf(String.valueOf(texteIntro.getText())))){
			SharedPreferences.Editor editor = settings.edit();
			editor.putString("score", String.valueOf(texteIntro.getText()));
			texteScore.setText("Personal record " + String.valueOf(texteIntro.getText()));
			editor.commit();
		}*/
	}
	
	View.OnClickListener mResetListener = new OnClickListener() {
		public void onClick(View v) {
			chronometre.setBase(SystemClock.elapsedRealtime());
			chronometre.stop();
			texteIntro.setText("0");
			i=1;
			init = false;
			finish = false;
			boutonScoreloopProfile.setVisibility(View.GONE);
			boutonSubmitScore.setVisibility(View.GONE);
		}
	};
				
	public void start(){
		chronometre.setBase(SystemClock.elapsedRealtime());
		chronometre.start(); 
		if (boutonRejouer.getVisibility() == View.GONE ){
			boutonRejouer.setVisibility(1);
		}
			
		boutonScoreloopProfile.setVisibility(View.GONE);
		boutonSubmitScore.setVisibility(View.GONE);
		init = true ;
	}

	public void addPreference(){
		SharedPreferences settings = getSharedPreferences("user_score", Context.MODE_PRIVATE);
		if (settings.getString("score", "") == ""  || (Integer.valueOf(settings.getString("score", "")) < Integer.valueOf(String.valueOf(texteIntro.getText())))){
			SharedPreferences.Editor editor = settings.edit();
			editor.putString("score", String.valueOf(texteIntro.getText()));
			texteScore.setText("Personal record " + String.valueOf(texteIntro.getText()));
			editor.commit();
		}
	}

	public void getPreference(){
		SharedPreferences settings = getSharedPreferences("user_score", Context.MODE_PRIVATE);
		if (settings.getString("score", "") == ""){
			texteScore.setText("No personal record");
			boutonSubmitScore.setVisibility(View.GONE);
		}else{
			texteScore.setText("Personal record " + settings.getString("score", ""));
		}
	}
	
	@Override
	public void onClick(View v) {
		
		switch (v.getId()){
		case R.id.btn_submit_score:
			SharedPreferences settings = getSharedPreferences("user_score", Context.MODE_PRIVATE);
			if (!(settings.getString("score", "") == "")){
				progressDialog.show();
				ScoreloopManagerSingleton.get().onGamePlayEnded(Double.valueOf(settings.getString("score", "")),null);
			}
			
			break;
		case R.id.btn_edit_profile:
			startActivity(new Intent(JustinBieberClic.this, EntryScreenActivity.class));
			break;
		}
			
	}

	@Override
	public void onScoreSubmit(int status, Exception error) {
		// TODO Auto-generated method stub
		startActivity(new Intent(this, ShowResultOverlayActivity.class));
		progressDialog.cancel();
	}

}